{% extends 'base.html' %}
{% block title %}
    <a class="navbar-brand" href="#"> 用户权限管理 </a>
{% endblock %}
{% block content %}
{# 使用 length 过滤器获取 staffs 变量的长度 #}

{% if current_user.has_role('Root') %}
    <div class="content">
        <h4 class="title">用户权限管理</h4>
        <div class="toolbar">

            <!--        Here you can write extra buttons/actions for the toolbar              -->
            <form method="post">
                查询方式
                <select name="info" required>
                    <option value="userID">员工号</option>
                    <option value="username">员工姓名</option>
                </select>
                <input type="text" name="date" autocomplete="off" required>
                <input type="submit" name="submit" value="查询">
            </form>

        </div>
        {% if info == 'userID' or info == 'username' %}
            <div class="material-datatables">
                <table id="datatables" class="table table-striped table-no-bordered table-hover"
                       style="width:100%;cellspacing:0">
                    <thead>
                    <tr>
                        <th>员工号</th>
                        <th>姓名</th>
                        <th>权限</th>
                        <th class="disabled-sorting text-right">操作</th>
                    </tr>
                    </thead>

                    <tbody>
                    <tr>
                        <td>{{ staffs.userID }}</td>
                        <td>{{ staffs.username }}</td>
                    {% for role in users.roles %}
                        <td>{{ role.name }}</td>
                        <td class="text-right">
							{% if role.name == 'User' %}
                            <a href="{{ url_for('promote_role',userID=staffs.userID) }}"
                               class="btn btn-simple btn-info btn-icon like">升为管理员</a>
							{% elif role.name == 'Admin' %}
                            <a href="{{ url_for('reduce_role',userID=staffs.userID) }}"
                               class="btn btn-simple btn-warning btn-icon edit">降为普通用户</a>
							{% endif %}
                        </td>
                    {% endfor %}
                    </tr>
                    </tbody>

                </table>
            </div>
        {% else %}

                <div class="material-datatables">
                    <table id="datatables" class="table table-striped table-no-bordered table-hover"
                           style="width:100%;cellspacing:0">
                        <thead>
                        <tr>
                            <th>员工号</th>
                            <th>姓名</th>
                            <th>权限</th>
                            <th class="disabled-sorting text-right">操作</th>
                        </tr>
                        </thead>

                        <tbody>
						{% for user in users %}
							{% for staff in staffs %}
								{% if user.userID == staff.userID %}
									{% for role in user.roles %}
										<tr>
											<td>{{ staff.userID }}</td>
											<td>{{ staff.username }}</td>
											<td>{{ role.name }}</td>
											<td class="text-right">
												{% if role.name == 'User' %}
													<a href="{{ url_for('promote_role',userID=staff.userID) }}"
													   class="btn btn-simple btn-info btn-icon like">升为管理员</a>
												{% elif role.name == 'Admin' %}
													<a href="{{ url_for('reduce_role',userID=staff.userID) }}"
													   class="btn btn-simple btn-warning btn-icon edit">降为普通用户</a>
											    {% endif %}
											</td>
										</tr>
									{% endfor %}
								{% endif %}
							{% endfor %}
						{% endfor %}
                        </tbody>

                    </table>
                </div>


        {% endif %}
    </div>
    {% endif %}
{% endblock %}